package wwiii;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(detachable = "true")
public class Area {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private String name;

	@Persistent
	private int x;

	@Persistent
	private int y;

	@Persistent
	private List<String> targets = new ArrayList<String>();

	@Persistent
	private String owner;

	@Persistent
	private int force;

	public Area(String name, int x, int y, Player owner, int force) {
		this.name  = name;
		this.x     = x;
		this.y     = y;
		this.owner = owner.getId();
		this.force = force;
	}

	public Key getKey() { return key; }
	public String getName() { return name; }
	public int getX() { return x; }
	public int getY() { return y; }

	public Collection<String> getTargets() { return targets; }
	public void addTarget(Area target) { targets.add(target.getName()); }
	public void removeTarget(Area target) { targets.remove(target.getName()); }

	public String getOwner() { return owner; }
	public void setOwner(Player owner) { this.owner = owner.getId(); }

	public int getForce() { return force; }
	public void setForce(int force) { this.force = force; }

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Area) {
			return equals((Area) obj);
		}
		return false;
	}
	private boolean equals(Area that) {
		if (key == null && that.key == null) return true;

		return key.equals(that.key);
	}

	@Override
	public int hashCode() {
		return key.hashCode();
	}

	@Override
	public String toString() {
		final ArrayList<String> links = new ArrayList<String>();

		for (String target : targets) {
			links.add(target);
		}

		return "[Area "+ key +" "+ name +" "+ links +"]";
	}
}
